/**
 Copyright (c) 2020-2022 宁波哥爱帮科技有限公司
 */
package cn.grab.fispservice.authentication.service.impl;

import cn.grab.fispapi.authentication.vo.PersonalAuthenticationVO;
import cn.grab.fispservice.authentication.mapper.PersonalAuthenticationMapper;
import cn.grab.fispservice.authentication.mapper.UserAuthenticationMapper;
import cn.grab.fispservice.authentication.model.PersonalAuthentication;
import cn.grab.fispservice.authentication.model.UserAuthentication;
import cn.grab.fispservice.authentication.service.PersonalAuthenticationService;
import cn.grab.fispservice.authentication.service.UserAuthenticationService;
import cn.grab.fispservice.util.ThreadLocalUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.fontana.base.result.Result;
import com.fontana.base.result.ResultCode;
import com.fontana.db.mapper.BaseDaoMapper;
import com.fontana.db.service.impl.AbstractBaseService;
import com.fontana.util.bean.BeanUtil;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @description 认证服务实现类
 * @author 16411
 * @date 2022/12/14 13:24
 */
@Service
public class PersonalAuthenticationServiceImpl extends AbstractBaseService<PersonalAuthentication,Long> implements
    PersonalAuthenticationService {

  @Resource
  private PersonalAuthenticationMapper personalAuthenticationMapper;

  @Resource
  private UserAuthenticationMapper userAuthenticationMapper;


  @Override
  protected BaseDaoMapper<PersonalAuthentication> mapper() {
    return personalAuthenticationMapper;
  }


  /**
   *@Param:
   *@return: PersonalAuthenticationVO 个人认证视图实体类
   *@Author: xxxx
   *@date: 2022/12/23
   */
  @Override
  public Result<Object> findPersonalAuthentication() {
    //获取当前登录的用户id
    Long userId = ThreadLocalUtils.get();
    if (null == userId ){
        return Result.failed(ResultCode.USER_NOT_LOGGED_IN,"当前未登录,请登录后重试!");
    }

    //根据当前用户id查询用户认证关联表信息
    UserAuthentication userAuthentication = userAuthenticationMapper.selectById(userId);

    //查询认证数据
    PersonalAuthentication personalAuthentication = personalAuthenticationMapper.selectById(
        userAuthentication.getPersonalAuthenticationId());

    //校验当前用户是否通过审核
    Integer whetherExamine = personalAuthentication.getWhetherExamine();
    if (whetherExamine == 0){
      return Result.failed("请等待管理员审核认证");
    } else if (whetherExamine == 1){
      return Result.failed("认证失败!请填写正确信息重新认证");
    }

    //拷贝并创建个人认证视图对象
    PersonalAuthenticationVO personalAuthenticationVO = BeanUtil.copy(personalAuthentication,
        PersonalAuthenticationVO.class);

    return Result.failed(personalAuthenticationVO,ResultCode.SUCCESS);
  }
}
